<!--
 * @Author: weikang
 * @Description:
 * @Date: 2024-01-07 15:12:50
 * @LastEditTime: 2024-02-25 22:26:15
 * @FilePath: \env-manager-pc\src\views\document\device\detail\model\lanya.vue
-->
<script setup lang="ts">
import { get } from 'lodash-es'
import { blue_tooth_label } from './page'
import { useBlueToothForm } from './page.helper'
import documentApi from '@/api/modules/document'

const { deviceId } = defineProps({
  deviceId: {
    type: String,
    default: '',
  },
})

const message = ref('')

const { blueToothFormData } = useBlueToothForm()

function getBlueToothData() {
  documentApi.DeviceGetBluetoothVersion({
    deviceObjID: deviceId,
  }).then((res) => {
    blueToothFormData.value.SoftVer = res.SoftVer
    blueToothFormData.value.SoftDate = res.SoftDate
    blueToothFormData.value.HardVer = res.HardVer
    blueToothFormData.value.HardDate = res.HardDate
  })
}

getBlueToothData()
const loading = ref(false)

async function callBlueToothData() {
  try {
    loading.value = true
    const resVersion = await documentApi.DeviceCallBluetoothVersion({ deviceObjID: deviceId })
    blueToothFormData.value.SoftVer = resVersion.SoftVer
    blueToothFormData.value.SoftDate = resVersion.SoftDate
    blueToothFormData.value.HardVer = resVersion.HardVer
    blueToothFormData.value.HardDate = resVersion.HardDate
    const resStatus = await documentApi.DeviceCallBluetoothStatus({ deviceObjID: deviceId })
    blueToothFormData.value.ConnCurStatus = resStatus.ConnCurStatus
    loading.value = false
  }
  catch (e) {
    console.error(e)
    message.value = get(e, 'response.data', '召测失败')
    loading.value = false
  }
}
</script>

<template>
  <div>
    <el-card class="box-card" shadow="never">
      <template #header>
        <div class="card-header">
          <span>蓝牙信息</span>
          <el-button type="primary" link :loading="loading" @click="callBlueToothData">
            召测
          </el-button>
        </div>
      </template>
      <el-form v-loading="loading" label-width="150px" label-suffix="：">
        <el-row>
          <el-col :span="12">
            <el-form-item label="软件版本">
              <el-input v-model="blueToothFormData.SoftVer" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="软件日期">
              <el-input v-model="blueToothFormData.SoftDate" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="硬件版本">
              <el-input v-model="blueToothFormData.HardVer" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="硬件日期">
              <el-input v-model="blueToothFormData.HardDate" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="当前状态">
              <el-select v-model="blueToothFormData.ConnCurStatus" disabled>
                <el-option v-for="(item, key) in blue_tooth_label" :key="key" :value="Number(key)" :label="item" />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </el-card>
  </div>
</template>

<style lang="scss" scoped>
.card-header {
  --at-apply: flex flex-justify-between;
}
</style>
